﻿#include "camera.h"
#include <QDebug>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>


#define FIRST_CAMERA_INDEX 1


bool enumCamera(char* serverName)
{
    int devicecount=SapManager::GetServerCount();
    if(devicecount<2)
    {
        qDebug()<<"no camera find";
        return false;
    }

    SapManager::GetServerName(FIRST_CAMERA_INDEX, serverName,CORSERVER_MAX_STRLEN);  //打开枚举到的第一个相机
    qDebug()<<serverName;

    return true;
}

bool creatObjects(char* serverName)
{
    m_loc=new SapLocation(serverName,0);
    m_acq=new SapAcqDevice(*m_loc,"C://Users//dk//Desktop//config//T_Linea_C2048-7um_test1_test2.ccf");            //采集设备，false采用默认配置
    m_buffer=new SapBuffer(1,m_acq);//缓存对象

   // m_buffer=new SapBuffer(1,2048,9400);
    m_buffer->SetWidth(2048);
    m_buffer->SetHeight(9400);
    m_conv=new SapColorConversion(m_buffer);         //bayer转换对象

    m_trans=new SapAcqDeviceToBuf(m_acq,m_buffer,onImageDataCallback,NULL);     //传输对象

    if(false==m_acq->Create())
    {
        destroyObjects();
        qDebug()<<"create acq device failed";
        return false;
    }

    if(false==m_buffer->Create())
    {
        destroyObjects();
        qDebug()<<"create buff failed";
        return false;
    }

    if(false==m_conv->Create())
    {
        destroyObjects();
        qDebug()<<"create Color Conversion failed";
        return false;
    }

    if(false==m_trans->Create())
    {
        destroyObjects();
        qDebug()<<"create transport failed";
        return false;
    }

    qDebug()<<"create object success";
    return true;
}

bool creatObjectsForLearn(char* serverName)
{
    m_loc=new SapLocation(serverName,0);
    m_acq=new SapAcqDevice(*m_loc,"C://Users//dk//Desktop//config//T_Linea_C2048-7um_test1_test2.ccf");            //采集设备，false采用默认配置
    //m_acq=new SapAcqDevice(*m_loc,false);
    m_buffer=new SapBuffer(1,m_acq);                 //缓存对象
    m_conv=new SapColorConversion(m_buffer);         //bayer转换对象

    m_trans=new SapAcqDeviceToBuf(m_acq,m_buffer,onImageForLearnCallback,NULL);     //传输对象

    if(false==m_acq->Create())
    {
        destroyObjects();
        qDebug()<<"create acq device failed";
        return false;
    }

    if(false==m_buffer->Create())
    {
        destroyObjects();
        qDebug()<<"create buff failed";
        return false;
    }

    if(false==m_conv->Create())
    {
        destroyObjects();
        qDebug()<<"create Color Conversion failed";
        return false;
    }

    if(false==m_trans->Create())
    {
        destroyObjects();
        qDebug()<<"create transport failed";
        return false;
    }

    qDebug()<<"create object success";
    return true;
}

void destroyObjects()
{
    if(m_trans && *m_trans) m_trans->Destroy();

    if(m_conv && *m_conv)  m_conv->Destroy();

    if(m_buffer && *m_buffer) m_buffer->Destroy();

    if(m_acq && *m_acq) m_acq->Destroy();
}

bool openCamera()
{
    char serverNmae[CORSERVER_MAX_STRLEN];
    if(!enumCamera(serverNmae))
    {
        qDebug()<<"enum camera failed";
        return false;
    }


    if(!creatObjects(serverNmae))
    {
        return false;
    }

    qDebug()<<"open camera success";
    return true;
}

bool openCameraForLearn()
{
    char serverNmae[CORSERVER_MAX_STRLEN];
    if(!enumCamera(serverNmae))
    {
        qDebug()<<"enum camera failed";
        return false;
    }

    if(!creatObjectsForLearn(serverNmae))
    {
        return false;
    }

    qDebug()<<"open camera success";
    return true;
}

void closeCamera()
{
    destroyObjects();
    if(m_trans)  delete m_trans;
    if(m_conv)   delete m_conv;
    if(m_buffer) delete m_buffer;
    if(m_acq)    delete m_acq;
}

void getCameraFeature()
{
    int featureCount;
    m_acq->GetFeatureCount(&featureCount);
    qDebug()<<featureCount;
    SapFeature feature(*m_loc);
    feature.Create();

    int featureIndex;
    for (featureIndex = 0; featureIndex < featureCount; featureIndex++)
    {
        char featureName[64];

        SapFeature::Type featureType;
        SapFeature::AccessMode featureAccessMode;

        m_acq->GetFeatureInfo(featureIndex, &feature);

        feature.GetName(featureName, sizeof(featureName));
        feature.GetType(&featureType);
        feature.GetAccessMode(&featureAccessMode);
        qDebug()<<featureIndex<<":"<<featureName<<", type"<<featureType;
    }
}



